package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/product")
public class AdminFileUploadController {

    @Value("${fileServer.url}")
    private String fileServer;

    @RequestMapping("/fileUpload")
    public Result<String> doFileUpload(@RequestPart("file") MultipartFile multipartFile) throws IOException {
        if(!multipartFile.isEmpty()){
            String fileUrl = FileUploadUtil.doFileUpload(multipartFile.getOriginalFilename(), multipartFile.getBytes());
            return Result.ok(fileServer+fileUrl);
        }
        return  Result.fail("你上传的图片为空，请重新上传");
    }
}
